import json
import re
import os
basic_path = str(os.path.dirname(__file__)) + "\\"
major_path = basic_path + "..\\..\\major\\major.json"
# 打开 JSON 文件
with open(major_path, 'r', encoding='utf-8') as f:
    data = json.load(f)


json_update = {}

for key, value in data.items():
    print(key)
    print(value['name'])
    try:
        key_new = value['code'][0:6]
        json_update[key_new] = {}
        json_update[key_new]['name'] = value['name']
        json_update[key_new]['hot'] = value['heat'].get('view_total', 0)
        json_update[key_new]['year'] = value['limit_year']
        json_update[key_new]['degree'] = value['degree']
        json_update[key_new]['is_what'] = value['is_what']
        json_update[key_new]['learn_what'] = value['learn_what']
        json_update[key_new]['do_what'] = value['do_what']

        s = value['content']
        s = re.sub("\n", "", s)
        s = re.sub("\r", "", s)
        s = re.sub(r"</p>", "\n", s)
        s = re.sub(r"<.*?>", "", s)
        # s = re.sub("\n", "<br>", s)

        json_update[key_new]['content'] = s

        json_update[key_new]['recommend_subject'] = value['sel_adv']
        json_update[key_new]['postgraduate'] = value['direction']
        json_update[key_new]['subject'] = value['course']
        json_update[key_new]['people'] = value['celebrity']
        json_update[key_new]['salary_self'] = value['professionalsalary']['majorsalaryavg']
        json_update[key_new]['salary_average'] = value['professionalsalary']['allmajorsalaryavg']


        json_update[key_new]['Rate1'] = value['jobdetail']['3'][0]['rate']
        json_update[key_new]['Rate2'] = value['jobdetail']['3'][1]['rate']
        json_update[key_new]['Rate3'] = value['jobdetail']['3'][2]['rate']
        json_update[key_new]['Rate4'] = value['jobdetail']['3'][3]['rate']
        json_update[key_new]['Rate5'] = value['jobdetail']['3'][4]['rate']
        json_update[key_new]['Rate6'] = value['jobdetail']['3'][5]['rate']

        json_update[key_new]['Job1'] = value['jobdetail']['3'][0]['detail_pos']
        json_update[key_new]['Job2'] = value['jobdetail']['3'][1]['detail_pos']
        json_update[key_new]['Job3'] = value['jobdetail']['3'][2]['detail_pos']
        json_update[key_new]['Job4'] = value['jobdetail']['3'][3]['detail_pos']
        json_update[key_new]['Job5'] = value['jobdetail']['3'][4]['detail_pos']
        json_update[key_new]['Job6'] = value['jobdetail']['3'][5]['detail_pos']

        json_update[key_new]['rate1'] = value['jobdetail']['2'][0]['rate']
        json_update[key_new]['rate2'] = value['jobdetail']['2'][1]['rate']
        json_update[key_new]['rate3'] = value['jobdetail']['2'][2]['rate']
        json_update[key_new]['rate4'] = value['jobdetail']['2'][3]['rate']
        json_update[key_new]['rate5'] = value['jobdetail']['2'][4]['rate']

        json_update[key_new]['region1'] = value['jobdetail']['2'][0]['area']
        json_update[key_new]['region2'] = value['jobdetail']['2'][1]['area']
        json_update[key_new]['region3'] = value['jobdetail']['2'][2]['area']
        json_update[key_new]['region4'] = value['jobdetail']['2'][3]['area']
        json_update[key_new]['region5'] = value['jobdetail']['2'][4]['area']

        json_update[key_new]['men_rate'] = value['rate']
    except:
        pass

# 打开一个文件并写入 JSON 数据
with open('json/majorDetail.json', 'w', encoding='utf-8') as f:
    json.dump(json_update, f, ensure_ascii=False, indent=4)
